﻿using System;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.DOCUMENT.Components;
using ASPNETVN.PORTAL.Components.Share;
using Type=ASPNETVN.PORTAL.Modules.DOCUMENT.Components.Type;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;

namespace ASPNETVN.PORTAL.Modules.DOCUMENT.Controls
{
    public partial class AddEditItem : ApproveItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            base.OnInit(e);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Components.Item.DeleteItem(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {
            try
            {
                Components.Item item = new Components.Item();
                if (this.ItemID > 0)
                {
                    item = Components.Item.Load(this.ItemID);
                }

                item.CategoryID = Convert.ToInt32(DropDownListCategory.SelectedValue);
                item.TypeID = Convert.ToInt32(DropDownListType.SelectedValue);
                item.PublisherID = Convert.ToInt32(DropDownListPublisher.SelectedValue);
                item.Code = TextBoxCode.Text;
                item.PublishedDate = Convert.ToDateTime(TextBoxPublishedDate.Text);
                if (TextBoxAvailableDate.Text.Length > 0)
                {
                    item.AvailableDate = Convert.ToDateTime(TextBoxAvailableDate.Text);
                }
                item.SignedBy = TextBoxSigner.Text;
                item.Summary = TextBoxSummary.Text;

                if (this.ItemID == 0)
                {
                    item.CreatedDate = DateTime.Now;
                    item.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
                    this.ItemID = item.Insert();
                }
                else
                {
                    item.Update();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            string url = HttpUrlBuilder.BuildShareUrl(this.ModuleID, this.ItemID, "UploadFile");
            Response.Redirect(url);

        }

       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Category.BuildDropDownListControl(ref DropDownListCategory, this.ModuleID);
                DropDownListPublisher.DataSource = Publisher.SelectCollectionAll(this.ModuleID);
                DropDownListPublisher.DataBind();

                DropDownListType.DataSource = Type.SelectCollectionAll(this.ModuleID);
                DropDownListType.DataBind();

                if (this.ItemID > 0)
                {
                    Components.Item item = Components.Item.Load(this.ItemID);

                    if (item != null)
                    {
                        DropDownListCategory.SelectedValue = item.CategoryID.ToString();
                        DropDownListPublisher.SelectedValue = item.PublisherID.ToString();
                        DropDownListType.SelectedValue = item.TypeID.ToString();
                        TextBoxCode.Text = item.Code;
                        TextBoxPublishedDate.Text = item.PublishedDate.ToShortDateString();
                        if (item.AvailableDate.Year > 1753)
                        {
                            TextBoxAvailableDate.Text = item.PublishedDate.ToShortDateString();
                        }

                        TextBoxSigner.Text = item.SignedBy;
                        TextBoxSummary.Text = item.Summary;
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }
    }
}